import numpy as np
import random as rd
import matplotlib.pyplot as plt
import time

# 1.对比numpy与list
a = []
for i in range(100000):
    a.append(rd.random())

# %time time1=sum(a)
na = np.array(a)
# time2=na.sum()
# print(time1,time2)

# 2.ndarray的基本属性 shape,size,dtype...
print(na.shape, na.dtype)
na1 = np.array(a, dtype=np.float16)
print(na1.dtype)

# 3.生成数组
# (1)生成0,1数组-
# np.ones([2,3])
one = np.ones([2, 3])
print(one)

# (2)通过现有数组进行创建
# 理解深拷贝和浅拷贝 -- asarray,array
b = [[1, 2, 3], [2, 3, 4]]
print("b:", b)
b1 = np.array(b)
# 修改b1的值
b1[0, 0] = -1
print("b1:", b1, "b:", b)
b2 = np.asarray(b)
b2[0, 1] = -1
print("b2:", b2, "b:", b)

# b[0,0] = -2
# print("b2:", b2, "b1:", b1)

# (3)固定范围的数组
print(np.linspace(1, 10, 11), np.logspace(0, 2, 3), np.arange(1, 10))
# (4)随机数组
# np.random.rand()[0,1]
x = np.random.rand(4)
x1 = np.random.rand(3, 4)
print(x1)
x2 = np.random.uniform(0,1,10000)
# x2 = [1, 1, 1, 1, 2, 4, 5]
# bins的作用？-划分的区间数
plt.hist(x2)
plt.show()

c = np.random.normal(1.75,1,100000)
plt.hist(c,bins=10000)
plt.show()

# 数组去重
d = np.array([[1, 2, 3], [1, 2, 5]])
# --一维数组
print(np.unique(d))

# 基本运算
print(d > 1)
d[d > 1] = -1
print(d)
